using System.Windows.Forms;
using System.Xml;

namespace OpenComposite.Mapper
{
	public partial class formXmlTreeViewer : Form
	{
		public formXmlTreeViewer()
		{
			InitializeComponent();
		}

		public XmlDocument XmlDocument
		{
			get { return _xmldoc; }
			set
			{
				_xmldoc = value;
				treeXML.Nodes.Clear();
				if ( _xmldoc != null && _xmldoc.DocumentElement != null ) {
					treeXML.BeginUpdate();
					treeXML.Nodes.Add(fillTreeRecursive(_xmldoc.DocumentElement));
					treeXML.ExpandAll();
					treeXML.EndUpdate();
				}
			}
		}

		private TreeNode fillTreeRecursive(XmlNode xnCurrent)
		{
			TreeNode tn = new TreeNode(xnCurrent.Name);
			if ( xnCurrent.Attributes != null ) {
				foreach ( XmlAttribute xa in xnCurrent.Attributes ) {
					tn.Text += string.Format(" {0}={1}", xa.Name, xa.Value);
				}
			}
			foreach ( XmlNode xn in xnCurrent.ChildNodes ) {
				tn.Nodes.Add(fillTreeRecursive(xn));
			}
			return tn;
		}

		private XmlDocument _xmldoc = null;
	}
}